package com.meeting.meetingroom_api.controller;

import com.meeting.meetingroom_api.dto.BookingDto;
import com.meeting.meetingroom_api.security.UserDetailsImpl;
import com.meeting.meetingroom_api.service.MaintenanceRefundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/maintenance")
public class MaintenanceController {

    private final MaintenanceRefundService maintenanceRefundService;

    @Autowired
    public MaintenanceController(MaintenanceRefundService maintenanceRefundService) {
        this.maintenanceRefundService = maintenanceRefundService;
    }

    /**
     * 手动触发维护退款处理
     */
    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/process-refunds")
    public ResponseEntity<Map<String, String>> processMaintenanceRefunds(
            @AuthenticationPrincipal UserDetailsImpl currentUser) {
        try {
            maintenanceRefundService.processMaintenanceRefunds();
            return ResponseEntity.ok(Map.of(
                "status", "success",
                "message", "维护退款处理任务已完成"
            ));
        } catch (Exception e) {
            return ResponseEntity.badRequest()
                .body(Map.of(
                    "status", "error",
                    "message", "维护退款处理失败: " + e.getMessage()
                ));
        }
    }

    /**
     * 检查特定会议室的维护冲突
     */
    @PreAuthorize("hasAnyRole('EMPLOYEE', 'ADMIN')")
    @GetMapping("/check-conflicts/{roomId}")
    public ResponseEntity<List<BookingDto>> checkMaintenanceConflicts(
            @PathVariable Integer roomId,
            @AuthenticationPrincipal UserDetailsImpl currentUser) {
        List<BookingDto> conflicts = maintenanceRefundService.checkMaintenanceConflicts(roomId);
        return ResponseEntity.ok(conflicts);
    }

    /**
     * 手动处理特定预订的维护退款
     */
    @PreAuthorize("hasAnyRole('EMPLOYEE', 'ADMIN')")
    @PostMapping("/refund/{bookingId}")
    public ResponseEntity<BookingDto> processMaintenanceRefund(
            @PathVariable Integer bookingId,
            @RequestParam(defaultValue = "会议室维护冲突，手动退款") String reason,
            @AuthenticationPrincipal UserDetailsImpl currentUser) {
        BookingDto result = maintenanceRefundService.processMaintenanceRefund(bookingId, reason);
        return ResponseEntity.ok(result);
    }

    /**
     * 获取所有需要维护退款的预订
     */
    @PreAuthorize("hasAnyRole('EMPLOYEE', 'ADMIN')")
    @GetMapping("/pending-refunds")
    public ResponseEntity<Page<BookingDto>> getPendingMaintenanceRefunds(
            @PageableDefault(size = 10) Pageable pageable,
            @AuthenticationPrincipal UserDetailsImpl currentUser) {
        Page<BookingDto> pendingRefunds = maintenanceRefundService.getPendingMaintenanceRefunds(pageable);
        return ResponseEntity.ok(pendingRefunds);
    }
} 